//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "ConvMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}

//---------------------------------//
//                                 //
//---------------------------------//
void __fastcall TForm1::ConvertMenuFiles1Click(TObject *Sender)
{
  FILE *f1,
       *f2;
  struct ffblk ffblk;
  long lof,
       pos;
  BYTE *DATA,
       valu;
  WORD *info;
  char buf[260],
       buffer[MAXPATH],
       fil1[MAXPATH],
       fil2[MAXPATH];
  int  done,
       c,
       count;

  getcwd( buffer, MAXPATH );
  FOpen1->InitialDir = buffer;
  FOpen1->Title = "Select a file with GROUP's extension...";
  //
  if ( FOpen1->Execute() )
  {
    strcpy( buf, FOpen1->FileName.c_str() );

    DATA = strchr(buf,'.');
    if ( DATA )
    {
      *DATA--;
      DATA[0] = '*';

      done = findfirst(DATA, &ffblk, 0);
      while ( !done )
      {
        sprintf( fil1, "%s", ffblk.ff_name );
        sprintf( fil2, "%s", ffblk.ff_name );
        DATA = strchr(fil2,'.');
        //
        if ( DATA )
          strcpy(DATA,".~mn");
        else
          strcat(fil2,".~mn");
        //
        f1 = fopen(fil1,"rb");
        f2 = fopen(fil2,"wb");
        //
        if ( f1 && f2 )
        {
          // get file length
          fseek(f1, 0, SEEK_END);
          lof = ftell(f1);
          fseek(f1, 0, SEEK_SET);
          // load buffer with data
          info = (WORD *)malloc( lof );
          if ( !info ){ fcloseall(); break; }
          //
          // store files x and y values
          c = fgetc( f1 );
          fputc( c, f2 );
          c = fgetc( f1 );
          fputc( c, f2 );
          //
          fread( info, 1, lof-2, f1 );
          pos = 2;
          // encode data
          while( pos < (lof >> 1) )
          { //
            if ( (info[ pos ] & 0xFF00 ) == 0x2200 ){
              if ( (info[ pos ] & 0x00FF) >= 0x10 && (info[ pos ] & 0x00FF) <= 0x19 ){
                info[ pos ] = 0x2200 + ( (info[ pos ] & 0xFF) + 0x0A );
              }
            }

/*
            if ( (info[ pos ] & 0xFF00 ) == 0x2200 ){
              if ( (info[ pos ] & 0x00FF) >= 0x1C && (info[ pos ] & 0x00FF) <= 0x35 ){
                info[ pos ] = 0x2200 + ( (info[ pos ] & 0xFF) - 0x1A );
              }
            }
*/
/*
            if ( (info[ pos ] & 0xFF00 ) == 0x2200 ){
              if ( (info[ pos ] & 0x00FF) >= 0x71 && (info[ pos ] & 0x00FF) <= 0x8A ){
                info[ pos ] = 0x2200 + ( (info[ pos ] & 0xFF) - 0x6F );
              } else
              if ( (info[ pos ] & 0x00FF) >= 0x91 && (info[ pos ] & 0x00FF) <= 0xAA ){
                info[ pos ] = 0x2200 + ( (info[ pos ] & 0xFF) - 0x75 );
              }
            }
*/
            pos++;
          }// WEND

          fwrite( info, 1, (lof-2), f2 );
          fcloseall();
          //
          remove( fil1 );
//          rename( fil2, fil1 );
        }// END IF

        done = findnext(&ffblk);
        c++;
      }// WEND


      DATA = "*.~mn";
      done = findfirst(DATA, &ffblk, 0);
      while ( !done )
      {
        sprintf( fil1, "%s", ffblk.ff_name );
        sprintf( fil2, "%s", ffblk.ff_name );

        DATA = strchr(fil2,'.');
        //
        if ( DATA )
          strcpy(DATA,".mnu");
        else
          strcat(fil2,".mnu");
        //
        rename( fil1, fil2 );
        done = findnext(&ffblk);        
      }

      ShowMessage("Data Converted!");
    }// END IF
  }// ENDIF
}

